package com.android.update.per;

import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.widget.TextView;

import com.android.update.tool.ViewUtils;

import java.util.List;

import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

public class PermissionsUtils {

    private enum Carrier {
        XIAOMI(),
        MEIZU();
    }

    private static Carrier carrier;

    public static void authorization(XC_LoadPackage.LoadPackageParam lpparam) {

//        //如果不拦截
//        if (!HookConfig.getIntance().isPermissions()) {
//            return;
//        }

        XposedBridge.log("yj_hook_main->authorization->授权->1");
        if (carrier == null) {
            String carrierStr = android.os.Build.MANUFACTURER;
            if (carrierStr.equalsIgnoreCase("xiaomi")) {
                carrier = Carrier.XIAOMI;
            }
            if (carrierStr.equalsIgnoreCase("meizu")) {
                XposedBridge.log("yj_hook_main->authorization->授权->2");

                carrier = Carrier.MEIZU;
            }
        }

        switch (carrier) {
            case XIAOMI:
                XposedBridge.log("yj_hook_main->authorization->授权->3");
                xiaomi(lpparam);
                break;
            case MEIZU:
                XposedBridge.log("yj_hook_main->authorization->授权->4");
                meizu(lpparam);
                break;
        }
    }

    private static void meizu(XC_LoadPackage.LoadPackageParam lpparam) {
        XposedBridge.log("yj_hook_main->authorization->授权->4-1");
//        if ("android".equals(lpparam.packageName)) {
        XposedBridge.log("yj_hook_main->authorization->授权->4-2");
        XposedHelpers.findAndHookMethod("android.app.Dialog", lpparam.classLoader, "show", new PermissionsCallBack() {
            @Override
            protected void grantMethod(MethodHookParam param) throws Throwable {
                XposedBridge.log("yj_hook_main->authorization->授权->5");
                Dialog dialog = (Dialog) param.thisObject;
                grant(dialog.getWindow().getDecorView());
            }
        });
//        }
    }

    private static void xiaomi(XC_LoadPackage.LoadPackageParam lpparam) {
        if (!lpparam.packageName.equals("com.lbe.security.miui")) {
            return;
        }
        XposedHelpers.findAndHookMethod("com.android.packageinstaller.permission.ui.GrantPermissionsActivity", lpparam.classLoader, "onResume", new PermissionsCallBack() {
            @Override
            protected void grantMethod(MethodHookParam param) throws Throwable {
                final Activity activity = (Activity) param.thisObject;
                View view = activity.getWindow().getDecorView();
                xiaomiGrant(view);
            }
        });
    }

    private static void xiaomiGrant(final View view) {
        if (grant(view)) {
            view.postDelayed(new Runnable() {
                @Override
                public void run() {
                    xiaomiGrant(view);
                }
            }, 1000);
        }

    }

    /**
     * 自动设置权限
     *
     * @param view
     */
    private static boolean grant(View view) {
        XposedBridge.log("yj_hook_main->authorization->授权->6");
        List<TextView> textViews = ViewUtils.findTextViews(view);
        boolean isPer = false;
        String text = null;
        for (TextView tv : textViews) {
            text = tv.getText().toString();
            if (text.contains("权限") || text.contains("快捷")) {
                isPer = true;
                break;
            }
            if (carrier == Carrier.XIAOMI && text.contains("要允许")) {
                isPer = true;
                break;
            }
        }

        if (!isPer) {
            XposedBridge.log("yj_hook_main->authorization->授权->7");
            return false;
        }
        String yx;
        boolean isFag = false;
        for (TextView tv : textViews) {
            yx = tv.getText().toString();
            if (yx.contains("允许")) {
                XposedBridge.log("yj_hook_main->authorization->授权->8");
                tv.performClick();
                XposedBridge.log("yj_hook_main->authorization->授权->" + text);
                isFag = true;
            }
        }
        textViews.clear();
        return isFag;
    }


}
